from django.shortcuts import render,redirect
from mes import models
from urllib import request
from django.shortcuts import render
#连接mysql的类
import pymysql
import pymssql
#引入项目的settings文件
from django.conf import settings


#写进数据库，把输入的SN写进jd_barcode.txt
def JD_BarcodePrint(request):
    import os
    data = []
    if request.method == 'GET':
        print("传入模式是GET")
    else:
        sn = str(request.POST.get("sn"))
        print(sn)
        print(type(sn))
        # 连接sqlserver数据库
        sql_server_conn = pymssql.connect(host='192.168.118.128', user='barcode', password='barcodefornexcom',
                                      database='MIS')
        sql_server_conn_insert = sql_server_conn.cursor()
        sql = "insert into test_event(UserName) values('%s')" % (sn,)
        sql_server_conn_insert.execute(sql)
        sql_server_conn.commit()
        sql_server_conn_insert.close()
        data.append(sn)

        #写入txt文本


        barcode_txt = open("jd_barcode.txt",'a')
        barcode_txt.write(sn + ",")
        barcode_txt.close()

    return render(request,"JD_BarcodePrint.html",{'data':data})










